package org.example.demo2miniotest.service;

import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.InputStream;

/**
 * @ClassName MinioService
 * @Description TODO
 * @Author Kx
 * @Date 2025/9/15 19:36
 * @Version 1.0
 */
@Service
public class MinioService {
    @Autowired
    private MinioClient minioClient;
    @Value("${minio.bucket-name}")
    private String bucketName;

    /**
     * @author: kx
     * @date: 2025/9/15 19:39
     * @param path : 路径
     * @param contentType : 文件类型
     * @param size : 文件大小
     * @param input : 文件流
     * @description: 上传文件
    */
    public void upload(String path, String contentType, Long size, InputStream input) {
        try {
            minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(path).contentType(contentType).stream(input, size, -1).build());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
